home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include <Vk/VkApp.h>
- #include <Vk/VkMenuBar.h>
- #include <Vk/VkSubMenu.h>
- #include <Xm/Form.h>
- #include <Xm/RowColumn.h>
- #include <stdlib.h>
- #include <limits.h>
- #include <libgen.h>
- #include <stdio.h>
- #include <time.h>
- #include "security.h"
- #include <sys/signal.h>
- #include <siginfo.h>
- #define MAX(a,b) (((a)>(b))?(a):(b))
-
- extern VkApp *securityApp;
- extern int devicenum;
- extern char *flipImage(char *,int,int);
-
- #ifdef SAVER
- extern "C" void sigSaver(Widget, int);
- #endif
-
- #ifdef NOSHIP
- #include <MovieController.h>
- #endif
-
- #ifdef DEBUG
- #define dprintf printf
- #else
- #define dprintf 0&&
- #endif
-
- SecurityWin::~SecurityWin()
- {
- delete _pref;
- delete _startStop;
- delete _compress;
- delete _visual;
- delete _checkBox;
- delete _fileMenu;
- delete _mainMenu;
- }
-
- const char *SecurityWin::className() { return "SecurityWin";}
-
- void SecurityWin::quitCallback( Widget, XtPointer, XtPointer)
- {
- theApplication->quitYourself();
- }
-
- SecurityWin::SecurityWin(const char *name) : VkWindow(name)
- {
- Arg args[10];
- int n;
- _pref = new Pref("preferences");
- Widget _form = XmCreateForm(mainWindowWidget(),"form",NULL,0);
- _compress = new CompressPanel("cmenu",_form);
- _compress->setFileName("security.mv");
- _compress->setFrameRate(-1.0);
- n = 0;
- XtSetArg(args[n],XmNtopAttachment, XmATTACH_FORM);n++;
- XtSetArg(args[n],XmNrightAttachment, XmATTACH_FORM);n++;
- XtSetArg(args[n],XmNleftAttachment, XmATTACH_FORM);n++;
- XtSetValues(_compress->baseWidget(), args, n);
- _compress->show();
- _checkBox = new VkCheckBox("misc",_form);
- _checkBox->addItem("Audio",TRUE);
- _checkBox->addItem("Monitor",FALSE);
- _checkBox->addItem("Saver",TRUE);
- #ifdef DPS
- _checkBox->addItem("Timestamp",FALSE);
- #endif
- n = 0;
- XtSetArg(args[n],XmNtopAttachment, XmATTACH_WIDGET);n++;
- XtSetArg(args[n],XmNtopWidget, _compress->baseWidget());n++;
- XtSetArg(args[n],XmNrightAttachment, XmATTACH_FORM);n++;
- XtSetArg(args[n],XmNleftAttachment, XmATTACH_FORM);n++;
- XtSetValues(_checkBox->baseWidget(), args, n);
- _checkBox->show();
- _visual = new VkRadioBox("viz",_form);
- _visual->addItem("Full");
- _visual->addItem("Half",TRUE);
- _visual->addItem("Quarter");
- n = 0;
- XtSetArg(args[n],XmNtopAttachment, XmATTACH_WIDGET);n++;
- XtSetArg(args[n],XmNtopWidget, _checkBox->baseWidget());n++;
- XtSetArg(args[n],XmNrightAttachment, XmATTACH_FORM);n++;
- XtSetArg(args[n],XmNleftAttachment, XmATTACH_FORM);n++;
- XtSetValues(_visual->baseWidget(), args, n);
- _visual->show();
- _startStop = new StartStopPanel("panel", _form);
- _startStop->addCallback(StartStopPanel::actionCallback,
- this,
- (VkCallbackMethod)&SecurityWin::statusChanged);
- n = 0;
- XtSetArg(args[n],XmNtopAttachment, XmATTACH_WIDGET);n++;
- XtSetArg(args[n],XmNtopWidget, _visual->baseWidget());n++;
- XtSetArg(args[n],XmNrightAttachment, XmATTACH_FORM);n++;
- XtSetArg(args[n],XmNleftAttachment, XmATTACH_FORM);n++;
- XtSetValues(_startStop->baseWidget(), args, n);
- _startStop->show();
- _timer = NULL;
- _movie = NULL;
- _monitor = NULL;
- _isBusy = FALSE;
- addView(_form);
- buildMenu();
- #ifdef QT
- mvInitQuickTime();
- #endif
- VkAddCallbackMethod(VkPrefDialog::prefCallback,
- _pref,
- this,
- &SecurityWin::preference, (void *)NULL);
- }
-
- void SecurityWin::buildMenu()
- {
- _mainMenu = new VkMenuBar((Boolean)TRUE);
- _fileMenu = addMenuPane("File");
- _fileMenu->addAction("Preferences",&SecurityWin::postPrefCallback, (XtPointer)this);
- #ifdef NOSHIP
- _fileMenu->addAction("View",&SecurityWin::viewCallback, (XtPointer)this);
- #endif
- _fileMenu->addAction("Quit", &SecurityWin::quitCallback, (XtPointer)this);
- _mainMenu->addSubmenu(_fileMenu);
- setMenuBar(_mainMenu);
- }
-
- void SecurityWin::preference(VkComponent *obj, void *clientData, void *callData)
- {
- Pref *pref = (Pref *)obj;
- SecurityWin *win = (SecurityWin *)clientData;
- VkDialogManager::VkDialogReason reason = (VkDialogManager::VkDialogReason)callData;
- if(reason == VkDialogManager::CANCEL) return;
-
- pref->update();
- }
-
- void SecurityWin::postPrefCallback(Widget, XtPointer clientData, XtPointer)
- {
- SecurityWin *obj = (SecurityWin *) clientData;
- obj->postPref();
- }
-
- #ifdef NOSHIP
- void SecurityWin::viewCallback(Widget, XtPointer clientData, XtPointer)
- {
- SecurityWin *obj = (SecurityWin *)clientData;
- obj->view();
- }
-
- void SecurityWin::view()
- {
- char *name;
- name = _compress->getFileName();
- if(!mvIsMovieFile(name)){
- fprintf(stderr,"%s is not a movie\n",name);
- return;
- }
- VkSimpleWindow *win = new VkSimpleWindow("viewPlayer");
- MovieController *viewWidget = new MovieController(win->mainWindowWidget(),
- "view",name,1,FALSE);
- win->setTitle(basename(name));
- viewWidget->show();
- win->show();
- }
- #endif
-
- void SecurityWin::postPref()
- {
- _pref->post("");
- }
-
- void SecurityWin::statusChanged(VkCallbackObject *, void *, void *callData)
- {
- char *ptr;
- Action currentAction = (Action) callData;
- switch (currentAction){
- case START:
- if(_timer == NULL){
- buildVideo();
- _timer = new VkPeriodic(_pref->getDelay() * 1000);
- VkAddCallbackMethod(VkPeriodic::timerCallback,
- _timer, this,
- &SecurityWin::grabFrame, (void *)NULL);
- ptr = _compress->getFileName();
- _movie = new NewMovie(ptr, 4.0,
- _video->getWidth(),
- _video->getHeight(),
- _compress->getCurrentCompressor());
- free(ptr);
- #ifdef DPS
- _title = new Title(mainWindowWidget(),
- _video->getWidth(),
- _video->getHeight());
- _title->setColor(1.0, 0.0, 0.0);
- _title->setOffset( _video->getWidth()/2, _video->getHeight()/2);
- #endif
- if(_checkBox->getValue(1)){
- _monitor = new Monitor(_video->getWidth(), _video->getHeight());
- }
- _sound = 0;
- _timer->start();
- }
- break;
- case STOP:
- if(_timer != NULL){
- _timer->stop();
- VkRemoveCallbackMethod(VkPeriodic::timerCallback,
- _timer, this,
- &SecurityWin::grabFrame, (void *)NULL);
- #ifdef DPS
- delete _title;
- _title = NULL;
- #endif
- delete _timer;
- _timer = NULL;
- delete _monitor;
- _monitor = NULL;
- // reset frame rate in movie based on _sound
- _movie->setNumAudio(_sound);
- delete _movie;
- _movie = NULL;
- _video->freeBuffer();
- delete [] _RGBFrame;
- delete _video;
- _video = NULL;
- _motion->freeBuffer();
- delete [] _newFrame;
- delete [] _oldFrame;
- delete _motion;
- _motion = NULL;
- }
- break;
- default:
- break;
- }
- }
-
- void SecurityWin::grabFrame(VkCallbackObject *, void *, void *)
- {
- time_t t;
- void *frame;
- Audio *audio;
- int diffs, newSounds;
- if(_isBusy) {
- fprintf(stderr,"Got a spurious timer event\n");
- return;
- }
- _isBusy = TRUE;
- audio = NULL;
- _motion->loadFrame(_motion->getWidth(), _motion->getHeight(), _newFrame);
- diffs = diffBuffer((unsigned char *)_oldFrame, (unsigned char *)_newFrame,
- _motion->getSize(),
- _pref->getAccuracy(),
- _pref->getFrames());
- if(diffs){
- _video->loadFrame(_video->getWidth(), _video->getHeight(), _RGBFrame);
- if(_monitor){
- _monitor->add(_RGBFrame);
- }
- #ifdef DPS
- if(_checkBox->getValue(3)){
- _title->putImage(_RGBFrame);
- t = time(NULL);
- _title->text(asctime(localtime(&t)),32/getScale());
- frame = _title->getImage();
- if(frame){
- if(!_movie->setFrame(-1,frame)){
- stopOnError();
- return;
- }
- delete [] frame;
- }else{
- if(!_movie->setFrame(-1,_RGBFrame)){
- stopOnError();
- return;
- }
- }
- }else{
- if(!_movie->setFrame(-1,_RGBFrame)){
- stopOnError();
- return;
- }
- }
- #else
- if(!_movie->setFrame(-1,_RGBFrame)){
- stopOnError();
- return;
- }
- #endif
- if(_checkBox->getValue(2)){
- XForceScreenSaver(XtDisplay(mainWindowWidget()), ScreenSaverReset);
- dprintf("Unblanking screen\n");
- }
- securityApp->busy();
- int busyCount = 1;
- audio = new Audio();
- while(busyCount < _pref->getTripCount()){
- frame = _oldFrame; _oldFrame = _newFrame; _newFrame = frame;
- _video->loadFrame(_video->getWidth(), _video->getHeight(), _RGBFrame);
- if(_monitor) _monitor->add(_RGBFrame);
- if(!_movie->setFrame(-1,_RGBFrame)){
- _isBusy = FALSE;
- securityApp->notBusy();
- stopOnError();
- return;
- }
- _motion->loadFrame(_motion->getWidth(), _motion->getHeight(), _newFrame);
- diffs = diffBuffer((unsigned char *)_oldFrame, (unsigned char *)_newFrame,
- _motion->getSize(),
- _pref->getAccuracy(),
- _pref->getFrames());
- if(_pref->getTripType() == MOTION){
- if (diffs){
- busyCount = 0;
- }else{
- busyCount++;
- }
- }else{
- busyCount++;
- }
- if(_checkBox->getValue(0)){
- _movie->addAudio(audio);
- _sound = (int)_movie->getNumAudio();
- }else{
- void *foo = audio->getAudio(newSounds);
- _sound += newSounds;
- }
- }
- delete audio;
- securityApp->notBusy();
- _isBusy = FALSE;
- }
- frame = _oldFrame; _oldFrame = _newFrame; _newFrame = frame;
- _isBusy = FALSE;
- }
-
- #define PIECES 16
- #define ROLLCOUNT 8
- int SecurityWin::diffBuffer(unsigned char *buf1, unsigned char *buf2, int siz, int pacc, int facc)
- {
- int i, out;
- out = 0;
- int innerLoop = siz / PIECES / ROLLCOUNT;
- for(int j = 0; j < PIECES; j++){
- for(i=0; i<innerLoop; i++){
- if(((buf1[1] - buf2[1]) > pacc ) || ((buf2[1] - buf1[1]) > pacc )) out++;
- if(((buf1[3] - buf2[3]) > pacc ) || ((buf2[3] - buf1[3]) > pacc )) out++;
- if(((buf1[5] - buf2[5]) > pacc ) || ((buf2[5] - buf1[5]) > pacc )) out++;
- if(((buf1[7] - buf2[7]) > pacc ) || ((buf2[7] - buf1[7]) > pacc )) out++;
- buf1 += ROLLCOUNT; buf2 += ROLLCOUNT;
- }
- if(out > facc){
- dprintf("The diff on iter %d = %d \n",j,out);
- return TRUE;
- }
- }
- dprintf("The diff = %d \n",out);
- return FALSE;
- }
- #undef ROLLCOUNT
- #undef PIECES
-
- void SecurityWin::buildVideo()
- {
- int scale = getScale();
- _motion = new VideoIn(2,devicenum,FALSE);
- _video = new VideoIn(scale,devicenum);
- if(!_video->hasVideo()){
- fprintf(stderr,"No video input.\n");
- exit(1);
- }
- _RGBFrame = new unsigned char [_video->getSize()];
- _oldFrame = new unsigned char [_motion->getSize()];
- _newFrame = new unsigned char [_motion->getSize()];
- _video->getBuffer();
- _motion->getBuffer();
- _motion->loadFrame(_motion->getWidth(), _motion->getHeight(), _oldFrame);
- }
-
- int SecurityWin::getScale()
- {
- int scale = 1;
- if(_visual->getValue(0) > 0) scale = 1;
- if(_visual->getValue(1) > 0) scale = 2;
- if(_visual->getValue(2) > 0) scale = 4;
- return scale;
- }
-
- void SecurityWin::stopOnError()
- {
- XButtonEvent xev1, xev2;
- Display *dpy;
- Widget w;
- w = _startStop->_stopButton;
- dpy = XtDisplay(w);
- // issue a STOP event to StartStop to end the
- // record session
- xev1.type = ButtonPress;
- xev2.type = ButtonRelease;
- xev1.send_event = xev2.send_event = TRUE;
- xev1.display = xev2.display = dpy;
- xev1.window = xev2.window = XtWindow(w);
- xev1.x = xev1.y = xev2.x = xev2.y = 0;
- xev1.x_root = xev1.y_root = xev2.x_root = xev2.y_root = 0;
- xev1.state = 0;
- xev2.state = 1;
- xev1.button = xev2.button = Button1;
- xev1.same_screen = xev2.same_screen = TRUE;
- XtDispatchEvent( (XEvent *)&xev1);
- XtDispatchEvent( (XEvent *)&xev2);
- XFlush(dpy);
- }
-